package com.panopset.sf.apps.blackjack;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;

import javax.swing.JPanel;

import com.panopset.Util;
import com.panopset.compat.Strings;
import com.panopset.game.blackjack.BlackjackConfig;
import com.panopset.game.card.Card;
import com.panopset.sf.ComponentFactory;
import com.panopset.sf.TitledTextArea;
import com.panopset.sf.Windoc;

public class StackedDeckPanel {

    public StackedDeckPanel(BlackjackConfig config, final Windoc windoc) {
        c = config;
        this.w = windoc;
        cf = w.cf;
    }

    private CardSelector cardSelector;

    private CardSelector getCardSelector() {
        if (cardSelector == null) {
            cardSelector = new CardSelector(c);
            cardSelector
                    .addSelectionListener(new CardSelector.SelectionListener() {
                        @Override
                        public void selected(Card card) {
                            getStackedDeckTA().append(
                                    card.name().toLowerCase().replace("_", " ")
                                            + Strings.getEol());
                        }
                    });
        }
        return cardSelector;
    }

    private TitledTextArea sd;

    private TitledTextArea getStackedDeckTA() {
        if (sd == null) {
            sd = w.cf.createTitledTextArea(w,
                    Util.x("Stacked deck"),
                    Util.x("Stack the deck with these cards for testing."), "",
                    "sd");
        }
        return sd;
    }

    public Component getPanel() {
        JPanel p = w.cf.createPanel(new BorderLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        p.add(BorderLayout.CENTER, getStackedDeckTA().getPanel());
        p.add(BorderLayout.SOUTH, getCardSelector().getPanel());
        p.setName("Testing");
        return p;
    }

    public String getText() {
        return getStackedDeckTA().getText();
    }

    public final Windoc w;
    final ComponentFactory cf;
    private final BlackjackConfig c;

}
